$(document).ready(function(){
    /* close popup */
    $('.popup .close').click(function(e){
        closePopup();
    });
    $('.popup').css('height',$('body').height());
    
    /* galerie*/
    if(typeof $('#galerie') !== 'undefined' && $('#galerie').length > 0){
        Galleria.loadTheme(baseUrl+themePath+'/galleria/galleria.classic.min.js');
        Galleria.configure({
            lightbox: true,
            thumbnails: 'numbers',
            autoplay: true,
            showInfo: false
        });
        Galleria.run('#galerie');
    }

    /* form contact */
    if(typeof $('#form_contact') !== 'undefined' && $('#form_contact').length > 0){
        var validatorForm = $('.form_user').validate({
            rules:{
                nom:'required'
                ,email:{
                    required:true,
                    email:true
                }
            }
            ,messages:{
                email:{
                    required:'Votre email est obligatoire'
                    ,email:'Indiquez une adresse email valide'
                }
                ,nom:'Votre nom est obligatoire'
            }
            ,submitHandler: function(form){
                form.submit();
            }
            ,invalidHandler: function(){
                $('div.validatError').remove();
            }
            ,focusInvalid: false
            ,errorElement: "div"
            ,errorClass: "validatError"
            ,errorPlacement: function(error, element) {
                if($('div#error'+element.attr('name'))){
                    $('div#error'+element.attr('name')).remove();
                }
                $('body').append(error);
                var offset = element.offset();
                error.attr('id','error'+element.attr('name'));
                error.css('left', offset.left + element.outerWidth());
                error.css('top', offset.top);
                error.click(function(){
                    $(this).fadeOut('fast');
                    $(this).remove();
                });
            }
        });
        $("input").focus(function() {
            $('div#error' + $(this).attr("name")).remove();
        });
    }
});

var nombreReserve,jourReserve,heureReserve,nomReserve,telReserve,mailReserve;

var openReservation = function(){
    $("#reservation #step1").hide();
    $("#reservation #step2").hide();
    $("#reservation #step3").hide();
    /* initialisation des variables */
    nombreReserve = 0;
    jourReserve = '';
    heureReserve = '';
    nomReserve = '';
    telReserve = '';
    mailReserve = '';
    
    $('#nombre_personne').val(0);
    $('#nom_resa').val('');
    $('#email_resa').val('');
    $('#tel_resa').val('');
    $("#reservation .recap_jour").html(jourReserve);
    $("#reservation .recap_heure").html(heureReserve);
    $("#reservation .recap_nbr_pers").html(nombreReserve);
    $("#reservation .recap_nom").html(nomReserve);
    $("#reservation .recap_mail").html(mailReserve);
    $("#reservation .recap_tel").html(telReserve);
    $('span.separateurInformations').show();
    $('span.confirmByMail').show();
    doAjax('getCalandar.ajax',{},showReservation);
};
var validReservation = function(){
    var datas = {};
    doAjax('validReservation.ajax',datas);
};

var reservationGoStep = function(step){
    var checked = false;
    if(step==2){
        checked = checkStep1();
    }if(step==3){
        checked = checkStep2();
        if(checked){
            validReservation();
        }
    }
    if(checked){
        $("#reservation #step1").hide();
        $("#reservation #step2").hide();
        $("#reservation #step3").hide();
        $("#reservation #step"+step).show();
        $(window).scrollTop(0);    
    }
};
var checkStep1 = function(){
    var retour = true;
    if($('#nombre_personne').val()==='' || !parseInt($('#nombre_personne').val()) || parseInt($('#nombre_personne').val())<1){
        retour = false;
        nombreReserve = 0;
        alert('Indiquez le nombre de personnes');
    }else{
        nombreReserve = parseInt($('#nombre_personne').val());
    }
    if(jourReserve==='' || heureReserve===''){
        retour = false;
        if(jourReserve==='' && heureReserve===''){
            alert("Indiquez le jour et l'heure");
        }else if(jourReserve===''){
            alert("Indiquez le jour");
        }else{
            alert("Indiquez le l'heure");
        }
    }
    if(retour){
        $("#reservation .recap_jour").html(jourReserve);
        $("#reservation .recap_heure").html(heureReserve);
        $("#reservation .recap_nbr_pers").html(nombreReserve);
    }
    return retour;
};
var checkStep2 = function(){
    var retour = true;
    if($('#nom_resa').val()===''){
        retour = false;
        nomReserve = '';
        alert("Indiquez votre nom");
    }else{
        nomReserve = $('#nom_resa').val();
    }
    if($('#email_resa').val()==='' && $('#tel_resa').val()===''){
        retour = false;
        alert("Indiquez votre téléphone OU votre adresse Email");
    }else{
        telReserve = $('#tel_resa').val();
        mailReserve = $('#email_resa').val();
    }

    if(retour){
        $("#reservation .recap_nom").html(nomReserve);
        $("#reservation .recap_mail").html(mailReserve);
        $("#reservation .recap_tel").html(telReserve);
        if(mailReserve===''){
            $('span.confirmByMail').hide();
            $('span.separateurInformations').hide();
        }
        if(telReserve===''){
            $('span.separateurInformations').hide();
        }
        
    }
    return retour;
};
var showReservation = function(datas){
    $('#reservation span.prev').unbind('click');
    $('#reservation span.next').unbind('click');
    
    $('#reservation span.prev').click(function(){ goPrevMonth(); });
    $('#reservation span.next').click(function(){ goNextMonth(); });
    
    $("#reservation #step1").show();
    
    processCalandar(datas);
    
    openPopup('reservation');
};
var processCalandar = function(datas){
    $('#reservation #jour span.current').html(datas.currentMonth);
    $('#reservation #jour table tbody').html(datas.htmlCalandar);
    
    checkJour();
    checkHeure();
};
var checkJour = function(){
    $('#reservation #jour table tbody td span').each(function(){
        $(this).removeClass('active');
        if(jourReserve!=='' && $(this).attr('date')===jourReserve){
            $(this).addClass('active');
        }
    });
    $('#reservation #jour table tbody td span').unbind('click');
    $('#reservation #jour table tbody td span').click(function(){
        $('#reservation #jour table tbody td span').each(function(){
            $(this).removeClass('active');            
        });
        $(this).addClass('active');
        jourReserve = $(this).attr('date');
    });
};
var checkHeure = function(){
    $('#reservation #heure table tbody td span').each(function(){
        $(this).removeClass('active');
        if(heureReserve!=='' && $(this).html()===heureReserve){
            $(this).addClass('active');
        }
    });
    $('#reservation #heure table tbody td span').unbind('click');
    $('#reservation #heure table tbody td span').click(function(){
        $('#reservation #heure table tbody td span').each(function(){
            $(this).removeClass('active');            
        });
        $(this).addClass('active');
        heureReserve = $(this).html();
    });
};

var goPrevMonth = function(){
    var data = {};
    data.action = 'prev';
    data.current = $('#reservation #jour span.current').html();
    doAjax('getCalandar.ajax',data,processCalandar);
};
var goNextMonth = function(){
    var data = {};
    data.action = 'next';
    data.current = $('#reservation #jour span.current').html();
    doAjax('getCalandar.ajax',data,processCalandar);
};

/* gestion des popups */
var openPopup = function(idPopup){
    $('.popup').hide();
    if( typeof $('#'+idPopup) !== 'undefined' && $('#'+idPopup).length>0 ){
        $('#'+idPopup).fadeIn('fast');
    }
};
var closePopup = function(){
    $('.popup').fadeOut('fast');
};

/* loader graphique */
var showAjaxLoader = function(){
    $('#ajax_loader').show();
    $('#ajax_loader').css('top',0);
};
var hideAjaxLoader = function(){
    $('#ajax_loader').css('top',-100);
};

var doAjax = function(target,data,callback,errorMsg,errorContainer){
    showAjaxLoader();
    $.ajax({
            type: "POST",
            url: target,
            data: data,
            dataType: 'json'
    })
    .always(function(){
        hideAjaxLoader();
    })
    .done(function(retour){
        if(retour.success){
            if(typeof callback !== 'undefined'){
                if(callback === 'reload') location.reload();
                else callback(retour.datas);
            }
        }else{
            if(errorContainer!=='')$(errorContainer).html(errorMsg);
            else alert(errorMsg);
        }
    })
    .fail(function(jqXHR, textStatus){
        alert('Erreur de requête : ' + textStatus);
    });
};